package Test06;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

//IO流练习
/*
    需求:把《出师表》的文章顺序进行恢复到一个新文件中。
 */
public class Test24Case01 {
    public static void main(String[] args) throws IOException {

        //1、读取数据
        BufferedReader br = new BufferedReader(new FileReader("D:\\工作\\src\\csb.txt"));
        ArrayList<String> list = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }

        //2、排序
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int i1 = Integer.parseInt(o1.split("\\.")[0]);
                int i2 = Integer.parseInt(o2.split("\\.")[0]);
                return i1 - i2;
            }
        });

        //3、写出数据
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\工作\\src\\csbCopy.txt"));
        for (String str : list) {
            bw.write(str);
            bw.newLine();
        }

        //4、释放资源
        bw.close();
        br.close();
    }
}
